var http  =  require('http');
var sys   =  require('util');
var url   =  require('url');
var fs    =  require('fs');
var zlib  =  require('zlib');
var parse =  require('./parse');
var util=require('util');
var querystring=require('querystring');
var path = require('path');

var logged = false;
http.createServer(function (req, res) {
  // Simple path-based request dispatcher
    switch (url.parse(req.url).pathname) {
        case '/':
            display_form(req, res);
            break;
        case '/login':
            login(req, res);
            break;
        case '/setup':
	    setup(req, res);
	    break;
        case '/storeSetup':
	    storeSetup(req, res);
	    break;
        case '/Graph':
	    generateGraph(req, res);
	    break;
        default:
            gotoServer(req, res);
            break;
    }



}).listen(3000, '127.0.0.1');

function gotoServer(request, response)
{
    var filePath = '.' + request.url;
    
    if (filePath == './')
        filePath = './index1.html';
         
    var extname = path.extname(filePath);
    var contentType = 'text/html';
    switch (extname) {
        case '.js':
            contentType = 'text/javascript';
            break;
        case '.css':
            contentType = 'text/css';
            break;
    }

    path.exists(filePath, function(exists) {
     
        if (exists) {
            fs.readFile(filePath, function(error, content) {
                if (error) {
                    response.writeHead(500);
                    response.end();
                }
                else {
                    response.writeHead(200, { 'Content-Type': contentType });
                    response.end(content, 'utf-8');
                }
            });
        }
        else {
            response.writeHead(404);
            response.end();
        }
    });
}


function display_form(req, res) {
  if(logged == true)
  {
     setup(req, res);
     return;
  }

  var fs = require('fs');
  fs.readFile('login.html', function(err,data){
    if(err) {
      console.error("Could not open file: %s", err);
      process.exit(1);
    }

    res.end(data);
    console.log(data);

  });
}

function login(req, res) {
  if(logged == true) {
     display_form(req, res);
     return;
  }

  if (req.method == 'POST') {
        var body = '';
        req.on('data', function (data) {
            body += data;
        });
        req.on('end', function () {
            var t = querystring.parse(body);
            if(t.username === "admin" && t.password === "1234")
	    {
  	       logged = true;
       	       res.end("Logged in");    
	    }
            else
            {
                display_form(req, res);
            }
        });
    }

}

function login(req, res) {
  if (req.method == 'POST') {
        var body = '';
        req.on('data', function (data) {
            body += data;
        });
        req.on('end', function () {
            var t = querystring.parse(body);
            if(t.username === "admin" && t.password === "1234")
	    {
  	       logged = true;
	    }

            display_form(req, res);
            
        });
    }
    else
    {
       display_form(req, res);
    }
}

function setup(req, res)
{
  if(logged == false) {
     display_form(req, res);
     return;
  }

  var fs = require('fs');
  fs.readFile('setup.html', function(err,data){
    if(err) {
      console.error("Could not open file: %s", err);
      process.exit(1);
    }

    res.end(data);
    console.log(data);

  });
}

function storeSetup(req, res)
{
  if(logged == false) {
     display_form(req, res);
     return;
  }

  if (req.method == 'POST') {
        var body = '';
        req.on('data', function (data) {
            body += data;
        });
        req.on('end', function () {
            var str = '';
            var t = querystring.parse(body);
            for(i = 0; i < t.operationsList.length; i++)
            {
               str += t.operationsList[i] + "\n";
            }
	    var fs = require('fs');
            fs.writeFile("setup.ps", str, function(err) {
              if(err) {
                 console.log(err);
              } else {
                 console.log("The file was saved!");
              }
            });
            var fs = require('fs');
	  fs.readFile('saved.html', function(err,data){
	    if(err) {
	      console.error("Could not open file: %s", err);
	      process.exit(1);
	    }

	    res.end(data);
	    console.log(data);

	  });
        });
    }
}

